package com.jic.product.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.WarehouseProductRelation;
import com.jic.product.manage.request.WarehouseProductRelationRequest;
import com.jic.product.manage.request.WarehouseProductRelationSaveRequest;
import com.jic.product.manage.request.WarehouseProductRequest;
import com.jic.product.manage.response.WarehouseProductRelationResponse;
import com.jic.product.mapper.ProductMapper;
import com.jic.product.mapper.WarehouseProductRelationMapper;
import com.jic.product.rest.response.ProductPropertyResponseFront;
import com.jic.product.rest.response.ProductPropertyValueResponse;
import com.jic.product.service.WarehouseProductRelationService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 仓库和商品关联表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-02-21 02:15:49
 */
@Service
public class WarehouseProductRelationServiceImpl implements WarehouseProductRelationService {

    @Autowired
    private WarehouseProductRelationMapper warehouseProductRelationMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public long insert(WarehouseProductRelation warehouseProductRelation) {
        long flag = warehouseProductRelationMapper.insert(warehouseProductRelation);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return warehouseProductRelationMapper.delete(id);
    }

    @Override
    public long update(WarehouseProductRelationRequest warehouseProductRelationRequest) {
        return warehouseProductRelationMapper.update(warehouseProductRelationRequest);
    }

    @Override
    public WarehouseProductRelation queryByPrimaryKey(Long id) {
        return warehouseProductRelationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<WarehouseProductRelation> query(WarehouseProductRelation warehouseProductRelation) {
        return warehouseProductRelationMapper.query(warehouseProductRelation);
    }

    @Override
    public PageResult<WarehouseProductRelationResponse> queryPage(WarehouseProductRelation warehouseProductRelation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<WarehouseProductRelationResponse> list = warehouseProductRelationMapper.queryPage(startIndex, pageQuery.getLimit(), warehouseProductRelation);
        if (list.size() != 0) {
            for (WarehouseProductRelationResponse warehouseProductRelationResponse : list) {
                List<ProductPropertyResponseFront> productPropertyResponseFrontList = productMapper.queryPropertyNameById(warehouseProductRelationResponse.getProductId());
                String property = "";
                for (ProductPropertyResponseFront productPropertyResponseFront : productPropertyResponseFrontList) {
                    property = property + productPropertyResponseFront.getName() + ":";
                    //根据商品属性名ids查询所有商品属性值数据
                    List<ProductPropertyValueResponse> ppv = productMapper.queryPropertyValueById(productPropertyResponseFront.getId());
                    if (CollectionUtils.isNotEmpty(ppv)) {
                        property = property + ppv.get(0).getValueName() + ";";
                    }
                }
                warehouseProductRelationResponse.setProperty(property);
            }
        }
        long count = warehouseProductRelationMapper.queryPageCount(warehouseProductRelation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    // 保存仓库和商品关联表
    @Override
    public RestResult<String> saveWarehouseProductRelation(WarehouseProductRelationSaveRequest warehouseProductRelationSaveRequest) {
        WarehouseProductRelation warehouseProductRelation = new WarehouseProductRelation();
        // 先for仓库集合 仓库一多商品
        for (Long warehouseId : warehouseProductRelationSaveRequest.getWarehouseIdList()) {
            // 再for商品集合
            for (WarehouseProductRequest wpr : warehouseProductRelationSaveRequest.getWarehouseProductRequestList()) {
                warehouseProductRelation.setWarehouseId(warehouseId);
                warehouseProductRelation.setProductId(wpr.getId());
                long wprCount = warehouseProductRelationMapper.queryWarehouseProductCount(warehouseProductRelation);
                if (wprCount > 0) {
                    // 仓库列表中存在重复的商品
                    continue;
                }
                // 先复制基础信息
                BeanUtils.copyProperties(warehouseProductRelationSaveRequest, warehouseProductRelation);
                warehouseProductRelation.setWarehouseId(warehouseId);
                warehouseProductRelation.setProductId(wpr.getId());
                warehouseProductRelation.setTransportWay(wpr.getTransportWay());
                warehouseProductRelation.setCreateTime(new Date());
                warehouseProductRelationMapper.insert(warehouseProductRelation);
            }
        }
        return RestResult.success("商品进目录成功!");
    }
}
